<script setup lang="ts">
import { ref, reactive } from "vue";
import { reqGetAttrInfoList } from "@/api/product/attr";
import {
  reqGetSpuImageList,
  reqGetHasSaleAttr,
  reqSaveSku,
} from "@/api/product/spu";
import { ElMessage } from "element-plus";
const $emit = defineEmits(["updataScene"]);
// 用于展示图片的el-table组件实例
let table = ref<any>();
// 销售属性列表
let attrInfoList = ref<any[]>([]);
// spu图片列表
let spuImageList = ref<any[]>([]);
// 已有的销售属性
let hasSaleAttr = ref<any[]>([]);
// 将来发请求时需要的sku参数
let skuParams = reactive<any>({
  category3Id: "",
  spuId: "",
  tmId: "",
  skuName: "",
  price: "",
  weight: "",
  skuDesc: "",
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: "",
});
// 点击取消按钮的回调
const cancel = () => {
  $emit("updataScene", 0);
};
// 初始化子组件数据的方法
const initSkuData = async (c1Id: any, c2Id: any, c3Id: any, spu: any) => {
  skuParams.category3Id = c3Id;
  skuParams.spuId = spu.id;
  skuParams.tmId = spu.tmId;
  let res1: any = await reqGetAttrInfoList(c1Id, c2Id, c3Id);
  if (res1.code == 200) {
    attrInfoList.value = res1.data;
  }
  let res2: any = await reqGetSpuImageList(spu.id);
  if (res2.code == 200) {
    spuImageList.value = res2.data;
  }
  let res3: any = await reqGetHasSaleAttr(spu.id);
  if (res3.code == 200) {
    hasSaleAttr.value = res3.data;
  }
};
// 点击设置为默认按钮的回调
const handleDefault = (row: any) => {
  spuImageList.value.forEach((item: any) => {
    // 全部图片的的复选框都不勾选
    table.value.toggleRowSelection(item, false);
  });
  // 勾选点击的那一项
  table.value.toggleRowSelection(row, true);
  skuParams.skuDefaultImg = row.imgUrl;
};
// 点击保存按钮的回调
const save = async () => {
  // 整理skuParams.skuAttrValueList参数
  skuParams.skuAttrValueList = attrInfoList.value.reduce(
    (prev: any, next: any) => {
      if (next.attrIdAndValueId) {
        let [attrId, valueId] = next.attrIdAndValueId.split(":");
        prev.push({
          attrId,
          valueId,
        });
      }
      return prev;
    },
    []
  );
  // 整理skuParams.skuSaleAttrValueList参数
  skuParams.skuSaleAttrValueList = hasSaleAttr.value.reduce(
    (prev: any, next: any) => {
      if (next.attrIdAndValueId) {
        let [attrId, valueId] = next.attrIdAndValueId.split(":");
        prev.push({
          attrId,
          valueId,
        });
      }
      return prev;
    },
    []
  );
  // 发请求
  let res: any = await reqSaveSku(skuParams);
  if (res.code == 200) {
    ElMessage({
      type: "success",
      message: "添加SKU成功",
    });
    // 回到场景0
    $emit("updataScene", 0);
  } else {
    ElMessage({
      type: "error",
      message: "添加SKU失败",
    });
  }
};
defineExpose({ initSkuData });
</script>
<script lang="ts">
export default {
  name: "SkuForm",
};
</script>

<template>
  <div>
    <el-form label-width="80px">
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
      </el-form-item>
      <el-form-item label="重量(g)">
        <el-input placeholder="重量(g)" v-model="skuParams.weight"></el-input>
      </el-form-item>
      <el-form-item label="SKU描述">
        <el-input
          type="textarea"
          placeholder="SKU描述"
          v-model="skuParams.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="attrInfoList.length > 0" label="平台属性">
        <el-form inline label-width="100px">
          <el-form-item
            v-for="attr in attrInfoList"
            :key="attr.id"
            :label="attr.attrName"
          >
            <!-- 收集到attrInfoList的每一项中去 -->
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
                :label="attrValue.valueName"
                :value="`${attrValue.attrId}:${attrValue.id}`"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性" v-if="hasSaleAttr.length > 0">
        <el-form inline label-width="100px">
          <el-form-item
            v-for="attr in hasSaleAttr"
            :key="attr.id"
            :label="attr.saleAttrName"
          >
            <el-select placeholder="请选择">
              <el-option
                v-for="attrValue in attr.spuSaleAttrValueList"
                :key="attrValue.id"
                :label="attrValue.saleAttrName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片名称">
        <el-table ref="table" border :data="spuImageList">
          <el-table-column
            type="selection"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="图片">
            <template v-slot="{ row, $index }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-button type="primary" @click="handleDefault(row)"
                >设置为默认</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped></style>
